<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button @click="handleAdd" icon="plus" style="margin-left: 8px" v-if="!formDisabled">新增</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <template v-if="!formDisabled">
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </template>
           <template v-else>
            <a @click="handleDetail(record)">详情</a>
          </template>
        </span>
      </a-table>
    </div>

    <section-add ref="modalForm" @ok="modalFormOk" />
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import SectionAdd from './SectionAdd'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { getAction } from '@/api/manage'
export default {
  name: 'SectionList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    SectionAdd,
  },
  props: {
    projectId: {
      type: String,
      default: '',
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      description: '项目标段信息管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        }, 
        {
          title: '标段（包）编号',
          align: 'center',
          dataIndex: 'bidSectionCode',
        },
        {
          title: '标段（包）名称',
          align: 'center',
          dataIndex: 'bidSectionName',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center', 
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/exProject/exProject/queryExSectionByMainId',
      },
      dictOptions: {},
      superFieldList: [],
      queryParam: {
        id: '',
      },
      loading: false,
    }
  },
  watch: {
    projectId(newVal) { 
      this.queryParam.id = newVal
      this.loadData(1)
    },
  },
  created() {
    this.queryParam.id = this.projectId
    this.getSuperFieldList()
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  methods: {
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'projectId', text: '项目id', dictCode: '' })
      fieldList.push({ type: 'string', value: 'tenderProjectCode', text: '招标项目编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bidSectionCode', text: '标段（包）编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'tenderProjectId', text: '招标项目标识码 (guid)', dictCode: '' })
      fieldList.push({ type: 'string', value: 'oldbidSectionId', text: '原标段（包）标识码 (guid)', dictCode: '' })
      fieldList.push({ type: 'string', value: 'iszhongzhi', text: '标段（包）状态 (暂停-1、终止-2)', dictCode: '' })
      fieldList.push({ type: 'string', value: 'zhongzhiInfo', text: '暂停/终止原因', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bidSectionName', text: '标段（包）名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'superviseDeptName', text: '监督部门名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'superviseDeptCode', text: '监督部门代码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'unifiedDealCode', text: '统一交易标识码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bidSectionContent', text: '标段（包）内容', dictCode: '' })
      fieldList.push({ type: 'string', value: 'tenderProjectClassifyCode', text: '标段（包）分类代码', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'contractReckonPrice', text: '标段合同估算价', dictCode: '' })
      fieldList.push({ type: 'string', value: 'currencyCode', text: '标段合同估算价币种代码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'priceUnit', text: '标段合同估算价单位', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bidQualification', text: '投标人资格条件', dictCode: '' })
      fieldList.push({ type: 'date', value: 'beginDate', text: '计划开工日期' })
      fieldList.push({ type: 'int', value: 'limiteTime', text: '工期 (单位：天)', dictCode: '' })
      fieldList.push({ type: 'string', value: 'attachmentCode', text: '附件关联标识号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'platformCode', text: '交易系统标识码', dictCode: '' })
      fieldList.push({ type: 'date', value: 'bidDate', text: '计划发包时间' })
      fieldList.push({ type: 'string', value: 'isWebBidding', text: '是否网上招投标 (是-1、否-0)', dictCode: 'yn' })
      this.superFieldList = fieldList
    },
    modalFormOk(row) {
      var that = this
      if (!row.sortNum) {
        row.sortNum = that.dataSource.length + 1
      }
      var obj = Object.assign({}, row)
      var index = that.dataSource.findIndex((item) => {
        if (item.id == row.id) {
          return true
        }
      })
      if (index > -1) {
        this.dataSource.splice(index, 1)
      }
      this.dataSource.push(obj)
      this.dataSource.sort(function (a, b) {
        return a.sortNum - b.sortNum
      })
      this.$emit('ok')
    },
    handleDelete: function (_id) {
      var that = this
      var index = that.dataSource.findIndex((item) => {
        if (item.id == _id) {
          return true
        }
      })
      if (index > -1) {
        this.dataSource.splice(index, 1)
        this.$emit('ok')
      }
    }, 
    loadData(arg) {
      if (this.queryParam.id == '') {
        return
      }

      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>